package com.jzo2o.customer.service.impl;


import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.controller.worker.BankController;
import com.jzo2o.customer.mapper.ServeProviderBankMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.domain.ServeProviderBank;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IServeProviderBankService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员及机构账户信息表 服务实现类
 * </p>
 *
 * @author hzzz
 * @since 2025-01-15
 */
@Service
public class ServeProviderBankServiceImpl extends ServiceImpl<ServeProviderBankMapper, ServeProviderBank> implements IServeProviderBankService {

    @Resource
    private IServeProviderService serveProviderService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAccount(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        if (ObjectUtils.isEmpty(bankAccountUpsertReqDTO)){
            throw new CommonException("添加失败");
        }

        ServeProviderBank serveProviderBank = BeanUtils.copyProperties(bankAccountUpsertReqDTO, ServeProviderBank.class);
        ServeProvider one = serveProviderService.lambdaQuery().eq(ServeProvider::getId, UserContext.currentUser().getId()).one();
        if (one == null){
            throw new CommonException("当前用户不存在");
        }

        serveProviderBank.setId(one.getId());
        serveProviderBank.setType(one.getType());

        ServeProviderBank byId = this.getById(UserContext.currentUser().getId());
        if (ObjectUtils.isEmpty(byId)){
            serveProviderBank.setCreateTime(LocalDateTime.now());
            this.save(serveProviderBank);
        }else {
            serveProviderBank.setCreateTime(byId.getCreateTime());
            serveProviderBank.setUpdateTime(LocalDateTime.now());
            this.updateById(serveProviderBank);
        }


    }

    @Override
    public ServeProviderBank getAccount() {
        return getById(UserContext.currentUserId());
    }
}
